//
//  ChatContentInfo.m
//  tcpoctest
//
//  Created by gump on 11/8/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "ChatContentInfo.h"

@implementation ChatContentInfo

-(id)init
{
    self = [super init];
    if (self)
    {
        self.strtext = @"";
        self.chatType = CT_TEXT;
        self.isSelf = false;
        self.mail = @"";
        self.isgroup = false;
        self.audioPlayLength = 0.0f;
        self.strChatDate = @"";
        self.iSequence = 0;
        self.strid = @"";
        self.ichatReadState = CRS_NULL;
        
        self.giftid = @"";
        self.igiftNum = 0;
        self.strsendmail = @"";
    }
    
    return self;
}

//把时间戳转化成时间
-(NSString *)GetTimeString
{
    double ftime = [self.strChatDate doubleValue] / 1000;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:ftime];
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];

    
    //获取日期
    NSCalendar * currentCalendar = [NSCalendar currentCalendar];//当前的日期
    NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;//分解的格式
    NSDateComponents * currentComponent = [currentCalendar components:unitFlags fromDate:[NSDate date]];//获得日期的组件
    NSDateComponents * lastComponent = [currentCalendar components:unitFlags fromDate:confromTimesp];//获取最后一条消息的组件
    int currentYear = [currentComponent year];//当前的年份
    int currentMonth = [currentComponent month];//当前的月份
    int currentDay = [currentComponent day];//当前的日期
    
    int lastInfoYear = [lastComponent year];//最后一条消息的年份
    int lastInfoMonth = [lastComponent month];//最后一条消息的月份
    int lastInfoDay = [lastComponent day];//最后一条消息的日期
    
    if (currentYear > lastInfoYear) {//当前年份大的时候
        [dateFormat setDateFormat:@"yyyy-MM-dd"];
        NSString * curdatestr = [dateFormat stringFromDate:confromTimesp];
        return curdatestr;
    }else if(currentYear == lastInfoYear){
        if (currentDay > lastInfoDay || currentMonth > lastInfoMonth) {//在日期不一样的时候，显示日期
            [dateFormat setDateFormat:@"MM-dd HH:mm"];
            NSString * curdatestr = [dateFormat stringFromDate:confromTimesp];
            return curdatestr;
        }else if(currentDay == lastInfoDay && currentMonth == lastInfoMonth){
            [dateFormat setDateFormat:@"HH:mm"];
            NSString * curdatestr = [dateFormat stringFromDate:confromTimesp];
            return curdatestr;
        }
    }
    
    return nil;
}

//显示最后一条聊天消息的时间
-(NSString *)getLastContentInfoTime{
    double ftime = [self.strChatDate doubleValue]/1000;//把服务器的十三位时间戳转化为十位的
    NSDate * confromTime = [NSDate dateWithTimeIntervalSince1970:ftime];//转化是格林威治时间
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
  
    //获取日期
    NSCalendar * currentCalendar = [NSCalendar currentCalendar];//当前的日期
    NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;//分解的格式
    NSDateComponents * currentComponent = [currentCalendar components:unitFlags fromDate:[NSDate date]];//获得日期的组件
    NSDateComponents * lastComponent = [currentCalendar components:unitFlags fromDate:confromTime];//获取最后一条消息的组件
    int currentYear = [currentComponent year];//当前的年份
    int currentMonth = [currentComponent month];//当前的月份
    int currentDay = [currentComponent day];//当前的日期
    
    int lastInfoYear = [lastComponent year];//最后一条消息的年份
    int lastInfoMonth = [lastComponent month];//最后一条消息的月份
    int lastInfoDay = [lastComponent day];//最后一条消息的日期

    if (currentYear > lastInfoYear) {//当前年份大的时候
        [dateFormat setDateFormat:@"yyyy-MM-dd"];
        NSString * curdatestr = [dateFormat stringFromDate:confromTime];
        return curdatestr;
    }else if(currentYear == lastInfoYear){
        if (currentDay > lastInfoDay || currentMonth > lastInfoMonth) {//在日期不一样的时候，显示日期
            [dateFormat setDateFormat:@"MM-dd"];
            NSString * curdatestr = [dateFormat stringFromDate:confromTime];
            return curdatestr;
        }else if(currentDay == lastInfoDay && currentMonth == lastInfoMonth){
            [dateFormat setDateFormat:@"HH:mm"];
            NSString * curdatestr = [dateFormat stringFromDate:confromTime];
            return curdatestr;
        }
    }
    
    return nil;
}



@end
